package com.ccit.common.exception;

import com.ccit.common.exception.fileUpload.*;
import com.ccit.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice //aop
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("执行全局异常Exception处理");
    }


    /*//特定异常处理
    @ExceptionHandler(GymException.class)
    @ResponseBody
    public Result error(GymException e) {
        e.printStackTrace();
        return Result.fail().message("执行特定异常GymException处理");
    }*/

    //自定义异常处理
    @ExceptionHandler(GymException.class)
    @ResponseBody
    public Result error(GymException e) {
        e.printStackTrace();
        return Result.fail(null).code(e.getCode()).message(e.getMessage());
    }


    //图片处理异常
    @ExceptionHandler(FileUploadException.class)
    public Result handleException(Throwable e) {
        Result result = new Result();
        if (e instanceof FileEmptyException) {
            result.setCode(6000);
        } else if (e instanceof FileSizeException) {
            result.setCode(6001);
        } else if (e instanceof FileTypeException) {
            result.setCode(6002);
        } else if (e instanceof FileStateException) {
            result.setCode(6003);
        } else if (e instanceof FileUploadIOException) {
            result.setCode(6004);
        }
        return result;
    }


}
